package com.shire.shire_interface.controller.friend;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.cs.WxChat;
import com.shire.shire_interface.model.cs.WxChatExample;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.friend.WxFriendExample;
import com.shire.shire_interface.model.tag.WxTag;
import com.shire.shire_interface.model.tag.WxTagExample;
import com.shire.shire_interface.model.tag.WxTagRecord;
import com.shire.shire_interface.model.tag.WxTagRecordExample;
import com.shire.shire_interface.service.chat.WxChatService;
import com.shire.shire_interface.service.friend.WxFriendService;
import com.shire.shire_interface.service.tag.WxTagRecordService;
import com.shire.shire_interface.service.tag.WxTagService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Api(tags = "聊天接口")
@RequestMapping("/chat")
@RestController
public class KfChatFriendController extends BaseController {

    @Resource
    private WxFriendService wxFriendService;
    @Resource
    private WxTagService wxTagService;
    @Resource
    private WxTagRecordService wxTagRecordService;
    @Resource
    private WxChatService wxChatService;

//    @ApiOperation(value = "修改好友备注 csId, wxAccount, friendWxAccount, friendRemark")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/editFriend")
    public Response editFriend(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String friendWxAccount = jsonObject.getString("friendWxAccount");
            String friendRemark = jsonObject.getString("friendRemark");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxFriendExample friendExample = new WxFriendExample();
            friendExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendWxAccount);
            WxFriend friend = new WxFriend();
            friend.setFriendremark(friendRemark);
            wxFriendService.updateByExampleSelective(friend,friendExample);


            WxChatExample wxChatExample = new WxChatExample();
            wxChatExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(friendWxAccount);
            WxChat wxChat = new WxChat();
            wxChat.setFriendremark(friendRemark);
            wxChatService.updateByExample(wxChat,wxChatExample);

            response.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            response.error("失败");
        }
        return response;
    }

//    @ApiOperation(value = "插入好友标签 friendAccount， type:'0：好友标签 1 群标签'")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/insertFriendTag")
    public Response insertFriendTag(@RequestBody WxTag wxTag, HttpSession session){
        Response response = new Response();
        try {
            String friendAccount = wxTag.getFriendAccount();

            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            wxTagService.insert(wxTag);

            Integer type = wxTag.getType();
            String wxaccunt = wxTag.getWxaccunt();
            WxTagRecord wxTagRecord = new WxTagRecord();

            Integer tagId = wxTag.getId();
            wxTagRecord.setTagid(tagId);
            wxTagRecord.setWxacount(wxaccunt);

            if(type.equals(0)){
                wxTagRecord.setAccountid(friendAccount);
                wxTagRecordService.insert(wxTagRecord);
            }
            if (type.equals(1)){
                wxTagRecord.setFriendGroupId(friendAccount);
                wxTagRecordService.insert(wxTagRecord);
            }
            response.success("成功");
            response.setData(tagId);
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "get好友标签csId, wxAccount, friendAccount, type 0：好友 1：群")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/friendTag")
    public Response getTagList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String friendAccount = jsonObject.getString("friendAccount");
            Integer type = jsonObject.getInteger("type");
            if (csId == null)
            {
                response.success("无CSID");
                return response;
            }
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxTagExample wxTagExample = new WxTagExample();
            wxTagExample.createCriteria().andCsidEqualTo(csId).andWxaccuntEqualTo(wxAccount).andTypeEqualTo(type);
            List<WxTag> wxTags = wxTagService.selectByExample(wxTagExample);

            WxTagRecordExample wxTagRecordExample = new WxTagRecordExample();

            if (type.equals(0)){
                wxTagRecordExample.createCriteria().andWxacountEqualTo(wxAccount).andAccountidEqualTo(friendAccount);
            }else if (type.equals(1)){
                wxTagRecordExample.createCriteria().andWxacountEqualTo(wxAccount).andFriendGroupIdEqualTo(friendAccount);
            }
            List<WxTagRecord> wxTagRecords = wxTagRecordService.selectByExample(wxTagRecordExample);

            List<Map<String,Object>> resultList = new ArrayList<>();

            for (WxTagRecord wxTagRecord : wxTagRecords) {
                Integer tagid = wxTagRecord.getTagid();
                for (WxTag wxTag : wxTags) {
                    Integer id = wxTag.getId();
                    if (tagid.equals(id)){
                        Map<String, Object> map = new HashMap<>();
                        map.put("id",wxTag.getId());
                        map.put("tagName",wxTag.getTagname());
                        resultList.add(map);
                    }
                }
            }
            response.success("成功");
            response.setData(resultList);
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "好友标签删除 id")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();

        try {

            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            Integer id = jsonObject.getInteger("id");
            wxTagService.delByPrimaryKey(id);
            WxTagRecordExample wxTagRecordExample = new WxTagRecordExample();
            wxTagRecordExample.createCriteria().andTagidEqualTo(id);
            wxTagRecordService.deleteByExample(wxTagRecordExample);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }

        return response;
    }

//    @ApiOperation(value = "更新 id， tagName")
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    public Response update(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer id = jsonObject.getInteger("id");
            String tagName = jsonObject.getString("tagName");
            WxTag wxTag = new WxTag();
            wxTag.setId(id);
            wxTag.setTagname(tagName);
            wxTagService.updateByPrimaryKeySelective(wxTag);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }

        return response;
    }

}
